﻿using System;
using FirebirdSql.Data.FirebirdClient;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Data;

namespace RMSM
{
	[Serializable()]
	public class HopDongBO : Csla.BusinessBase<HopDongBO>
	{
		#region Business Properties
		//HOPDONGID
		private int? _HopDongID = null;
		public int? HopDongID
		{
			get { return this._HopDongID; }
			set { this._HopDongID = value; PropertyHasChanged("HopDongID"); }
		}

		//TENKHACHHANG
		private string _TenKhachHang = null;
		public string TenKhachHang
		{
			get { return this._TenKhachHang; }
			set { this._TenKhachHang = value; PropertyHasChanged("TenKhachHang"); }
		}

		//TENKHACHHANG2
		private string _TenKhachHang2 = null;
		public string TenKhachHang2
		{
			get { return this._TenKhachHang2; }
			set { this._TenKhachHang2 = value; PropertyHasChanged("TenKhachHang2"); }
		}

		//DIACHI
		private string _DiaChi = null;
		public string DiaChi
		{
			get { return this._DiaChi; }
			set { this._DiaChi = value; PropertyHasChanged("DiaChi"); }
		}

		//DIENTHOAI
		private string _DienThoai = null;
		public string DienThoai
		{
			get { return this._DienThoai; }
			set { this._DienThoai = value; PropertyHasChanged("DienThoai"); }
		}

		//NGAYCUOI
		private DateTime? _NgayCuoi = DateTime.Now;
		public DateTime? NgayCuoi
		{
			get { return this._NgayCuoi; }
			set { this._NgayCuoi = value; PropertyHasChanged("NgayCuoi"); }
		}

		//CHUATHANHLY
		private bool? _ChuaThanhLy = true;
		public bool? ChuaThanhLy
		{
			get { return this._ChuaThanhLy; }
			set { this._ChuaThanhLy = value; PropertyHasChanged("ChuaThanhLy"); }
		}

		//TONGGIATRIHD
		private double? _TongGiaTriHD = null;
		public double? TongGiaTriHD
		{
			get { return this._TongGiaTriHD; }
			set { this._TongGiaTriHD = value; PropertyHasChanged("TongGiaTriHD"); }
		}

		//TONGGIATRIHDBC
		private string _TongGiaTriHDBC = null;
		public string TongGiaTriHDBC
		{
			get { return this._TongGiaTriHDBC; }
			set { this._TongGiaTriHDBC = value; PropertyHasChanged("TongGiaTriHDBC"); }
		}

		//SOTIENDATTRUOC
		private double? _SoTienDatTruoc = null;
		public double? SoTienDatTruoc
		{
			get { return this._SoTienDatTruoc; }
			set { this._SoTienDatTruoc = value; PropertyHasChanged("SoTienDatTruoc"); }
		}

		//SOTIENDATTRUOCBC
		private string _SoTienDatTruocBC = null;
		public string SoTienDatTruocBC
		{
			get { return this._SoTienDatTruocBC; }
			set { this._SoTienDatTruocBC = value; PropertyHasChanged("SoTienDatTruocBC"); }
		}

		//SOTIENCONLAI
		private double? _SoTienConLai = null;
		public double? SoTienConLai
		{
			get { return this._SoTienConLai; }
			set { this._SoTienConLai = value; PropertyHasChanged("SoTienConLai"); }
		}

		//SOTIENCONLAIBC
		private string _SoTienConLaiBC = null;
		public string SoTienConLaiBC
		{
			get { return this._SoTienConLaiBC; }
			set { this._SoTienConLaiBC = value; PropertyHasChanged("SoTienConLaiBC"); }
		}

		//TPTDTONGTIEN
		private double? _TPTDTongTien = null;
		public double? TPTDTongTien
		{
			get { return this._TPTDTongTien; }
			set { this._TPTDTongTien = value; PropertyHasChanged("TPTDTongTien"); }
		}

		//TPTDDATTRUOC
		private double? _TPTDDatTruoc = null;
		public double? TPTDDatTruoc
		{
			get { return this._TPTDDatTruoc; }
			set { this._TPTDDatTruoc = value; PropertyHasChanged("TPTDDatTruoc"); }
		}

		//TPTDCONLAI
		private double? _TPTDConLai = null;
		public double? TPTDConLai
		{
			get { return this._TPTDConLai; }
			set { this._TPTDConLai = value; PropertyHasChanged("TPTDConLai"); }
		}

		//TPTDCHITIETDATTRUOC
		private string _TPTDChiTietDatTruoc = null;
		public string TPTDChiTietDatTruoc
		{
			get { return this._TPTDChiTietDatTruoc; }
			set { this._TPTDChiTietDatTruoc = value; PropertyHasChanged("TPTDChiTietDatTruoc"); }
		}

		//CHTONGTIEN
		private double? _CHTongTien = null;
		public double? CHTongTien
		{
			get { return this._CHTongTien; }
			set { this._CHTongTien = value; PropertyHasChanged("CHTongTien"); }
		}

		//CHDATTRUOC
		private double? _CHDatTruoc = null;
		public double? CHDatTruoc
		{
			get { return this._CHDatTruoc; }
			set { this._CHDatTruoc = value; PropertyHasChanged("CHDatTruoc"); }
		}

		//CHCONLAI
		private double? _CHConLai = null;
		public double? CHConLai
		{
			get { return this._CHConLai; }
			set { this._CHConLai = value; PropertyHasChanged("CHConLai"); }
		}

		//CHCHITIETDATTRUOC
		private string _CHChiTietDatTruoc = null;
		public string CHChiTietDatTruoc
		{
			get { return this._CHChiTietDatTruoc; }
			set { this._CHChiTietDatTruoc = value; PropertyHasChanged("CHChiTietDatTruoc"); }
		}

		//QPTONGTIEN
		private double? _QPTongTien = null;
		public double? QPTongTien
		{
			get { return this._QPTongTien; }
			set { this._QPTongTien = value; PropertyHasChanged("QPTongTien"); }
		}

		//QPDATTRUOC
		private double? _QPDatTruoc = null;
		public double? QPDatTruoc
		{
			get { return this._QPDatTruoc; }
			set { this._QPDatTruoc = value; PropertyHasChanged("QPDatTruoc"); }
		}

		//QPCONLAI
		private double? _QPConLai = null;
		public double? QPConLai
		{
			get { return this._QPConLai; }
			set { this._QPConLai = value; PropertyHasChanged("QPConLai"); }
		}

		//QPCHITIETDATTRUOC
		private string _QPChiTietDatTruoc = null;
		public string QPChiTietDatTruoc
		{
			get { return this._QPChiTietDatTruoc; }
			set { this._QPChiTietDatTruoc = value; PropertyHasChanged("QPChiTietDatTruoc"); }
		}

		//DVKTONGTIEN
		private double? _DVKTongTien = null;
		public double? DVKTongTien
		{
			get { return this._DVKTongTien; }
			set { this._DVKTongTien = value; PropertyHasChanged("DVKTongTien"); }
		}

		//DVKDATTRUOC
		private double? _DVKDatTruoc = null;
		public double? DVKDatTruoc
		{
			get { return this._DVKDatTruoc; }
			set { this._DVKDatTruoc = value; PropertyHasChanged("DVKDatTruoc"); }
		}

		//DVKCONLAI
		private double? _DVKConLai = null;
		public double? DVKConLai
		{
			get { return this._DVKConLai; }
			set { this._DVKConLai = value; PropertyHasChanged("DVKConLai"); }
		}

		//DVKCHITIETDATTRUOC
		private string _DVKChiTietDatTruoc = null;
		public string DVKChiTietDatTruoc
		{
			get { return this._DVKChiTietDatTruoc; }
			set { this._DVKChiTietDatTruoc = value; PropertyHasChanged("DVKChiTietDatTruoc"); }
		}

		//NGAYLAMHD
		private DateTime? _NgayLamHD = DateTime.Now;
		public DateTime? NgayLamHD
		{
			get { return this._NgayLamHD; }
			set { this._NgayLamHD = value; PropertyHasChanged("NgayLamHD"); }
		}

		//MINDAY
		private DateTime? _MinDay = DateTime.Now;
		public DateTime? MinDay
		{
			get { return this._MinDay; }
			set { this._MinDay = value; PropertyHasChanged("MinDay"); }
		}

		//MAXDAY
		private DateTime? _MaxDay = DateTime.Now;
		public DateTime? MaxDay
		{
			get { return this._MaxDay; }
			set { this._MaxDay = value; PropertyHasChanged("MaxDay"); }
		}

		//MINDAYTT
		private DateTime? _MinDayTT = DateTime.Now;
		public DateTime? MinDayTT
		{
			get { return this._MinDayTT; }
			set { this._MinDayTT = value; PropertyHasChanged("MinDayTT"); }
		}

		//MAXDAYTT
		private DateTime? _MaxDayTT = DateTime.Now;
		public DateTime? MaxDayTT
		{
			get { return this._MaxDayTT; }
			set { this._MaxDayTT = value; PropertyHasChanged("MaxDayTT"); }
		}

		//DELE
		private bool? _DELE = false;
		public bool? DELE
		{
			get { return this._DELE; }
			set { this._DELE = value; PropertyHasChanged("DELE"); }
		}


		private int? _STT0 = 0;
		public int? STT0
		{
			get { return _STT0; }
			set { _STT0 = value; }
		}
		#endregion Business Properties
		//==========================================================================

		#region Factory Methods
		public void SetIsNew()
		{
			MarkNew();
		}

		public void SetIsOld()
		{
			MarkOld();
		}

		public void SetIsDirty()
		{
			MarkDirty();
		}

		public void SetIsChild()
		{
			MarkAsChild();
		}

		public object GetValue(string fieldName)
		{
			PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
			return propInfo.GetValue(this, null);
		}

		public void SetValue(string fieldName, object value)
		{
			PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
			propInfo.SetValue(this, value, null);
			MarkDirty();
		}

		protected override object GetIdValue()
		{
			return _HopDongID.Value;
		}

		private static int newHopDongID = -1;
		private HopDongBO()
		{
			_HopDongID = newHopDongID--;
		}

		public HopDongBO(FbDataReader dataReader)
		{
			MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public HopDongBO(FbDataReader dataReader, bool isRoot)
		{
			if (!isRoot)
				MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public static HopDongBO NewRootHopDongBO()
		{
			return DataPortal.Create<HopDongBO>(new RootCriteria());
		}

		internal static HopDongBO NewChildHopDongBO()
		{
			return DataPortal.Create<HopDongBO>(new ChildCriteria());
		}

		public static HopDongBO GetRootHopDongBO(int _HopDongID)
		{
			return DataPortal.Fetch<HopDongBO>(new RootCriteria(_HopDongID));
		}

		public static HopDongBO GetRootHopDongBOByWhere(string where)
		{
			return DataPortal.Fetch<HopDongBO>(where);
		}

		internal static HopDongBO GetChildHopDongBO(FbDataReader dataReader)
		{
			return new HopDongBO(dataReader);
		}

		public static void DeleteHopDongBO(int _HopDongID)
		{
			DataPortal.Delete(new RootCriteria(_HopDongID));
		}

		public static void DeleteHopDongBO(int _HopDongID, bool directDelete)
		{
			DataPortal.Delete(new RootCriteria(_HopDongID, directDelete));
		}

		public static string GetIDHopDongBO()
		{
			return "HopDongID";
		}
		#endregion Factory Methods
		//==========================================================================

		#region Criteria
		[Serializable()]
		public class RootCriteria
		{
			public int HopDongID;
			public bool directDelete = false;
			public RootCriteria(int _HopDongID)
			{
				this.HopDongID = _HopDongID;
				this.directDelete = false;
			}

			public RootCriteria(int _HopDongID, bool directDelete)
			{
				this.HopDongID = _HopDongID;
				this.directDelete = directDelete;
			}

			public RootCriteria() { }
		}

		[Serializable()]
		public class ChildCriteria
		{
			public int HopDongID;
			public bool directDelete = false;
			public ChildCriteria(int _HopDongID)
			{
				this.HopDongID = _HopDongID;
				this.directDelete = false;
			}

			public ChildCriteria(int _HopDongID, bool directDelete)
			{
				this.HopDongID = _HopDongID;
				this.directDelete = directDelete;
			}

			public ChildCriteria() { }
		}
		#endregion Criteria
		//==========================================================================

		#region Create
		[RunLocal]
		private void DataPortal_Create(RootCriteria criteria)
		{
		}

		private void DataPortal_Create(ChildCriteria criteria)
		{
			MarkAsChild();
			ValidationRules.CheckRules();
		}

		#endregion Create
		//==========================================================================

		#region Fetch
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_Fetch(RootCriteria criteria)
		{
			this.Execute_Fetch(criteria);
		}

		protected void DataPortal_Fetch(string where)
		{
			this.Execute_Fetch(where);
		}

		protected void Execute_Fetch(RootCriteria criteria)
		{
			this.This_Fetch(criteria);
		}

		protected void Execute_Fetch(string where)
		{
			this.This_Fetch(where);
		}

		private void This_Fetch(RootCriteria criteria)
		{
			using (FbConnection conn = new FbConnection(Session.CnnStr))
			{
				conn.Open();
				using (FbCommand cmd = conn.CreateCommand())
				{
					cmd.CommandText = "HopDongBO_B";
					cmd.CommandType = CommandType.StoredProcedure;
					cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = "HOPDONGID = " + criteria.HopDongID;

					using (FbDataReader reader = cmd.ExecuteReader())
					{
						if (reader.Read())
							Fetch(reader);
					}
				}
			}
		}

		private void This_Fetch(string where)
		{
			using (FbConnection conn = new FbConnection(Session.CnnStr))
			{
				conn.Open();
				using (FbCommand cmd = conn.CreateCommand())
				{
					cmd.CommandText = "HopDongBO_B";
					cmd.CommandType = CommandType.StoredProcedure;
					cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = where;

					using (FbDataReader reader = cmd.ExecuteReader())
					{
						if (reader.Read())
							Fetch(reader);
					}
				}
			}
		}

		private void Fetch(FbDataReader dataReader)
		{
			if (!(dataReader["HOPDONGID"] is DBNull))
				this._HopDongID = (int)dataReader["HOPDONGID"];
			if (!(dataReader["TENKHACHHANG"] is DBNull))
				this._TenKhachHang = ((string)dataReader["TENKHACHHANG"]).Trim();
			if (!(dataReader["TENKHACHHANG2"] is DBNull))
				this._TenKhachHang2 = ((string)dataReader["TENKHACHHANG2"]).Trim();
			if (!(dataReader["DIACHI"] is DBNull))
				this._DiaChi = ((string)dataReader["DIACHI"]).Trim();
			if (!(dataReader["DIENTHOAI"] is DBNull))
				this._DienThoai = ((string)dataReader["DIENTHOAI"]).Trim();
			if (!(dataReader["NGAYCUOI"] is DBNull))
				this._NgayCuoi = (DateTime)dataReader["NGAYCUOI"];
			if (!(dataReader["CHUATHANHLY"] is DBNull))
				this._ChuaThanhLy = (((string)dataReader["CHUATHANHLY"]).Trim() == "1");
			if (!(dataReader["TONGGIATRIHD"] is DBNull))
				this._TongGiaTriHD = Convert.ToDouble(dataReader["TONGGIATRIHD"]);
			if (!(dataReader["TONGGIATRIHDBC"] is DBNull))
				this._TongGiaTriHDBC = ((string)dataReader["TONGGIATRIHDBC"]).Trim();
			if (!(dataReader["SOTIENDATTRUOC"] is DBNull))
				this._SoTienDatTruoc = Convert.ToDouble(dataReader["SOTIENDATTRUOC"]);
			if (!(dataReader["SOTIENDATTRUOCBC"] is DBNull))
				this._SoTienDatTruocBC = ((string)dataReader["SOTIENDATTRUOCBC"]).Trim();
			if (!(dataReader["SOTIENCONLAI"] is DBNull))
				this._SoTienConLai = Convert.ToDouble(dataReader["SOTIENCONLAI"]);
			if (!(dataReader["SOTIENCONLAIBC"] is DBNull))
				this._SoTienConLaiBC = ((string)dataReader["SOTIENCONLAIBC"]).Trim();
			if (!(dataReader["TPTDTONGTIEN"] is DBNull))
				this._TPTDTongTien = Convert.ToDouble(dataReader["TPTDTONGTIEN"]);
			if (!(dataReader["TPTDDATTRUOC"] is DBNull))
				this._TPTDDatTruoc = Convert.ToDouble(dataReader["TPTDDATTRUOC"]);
			if (!(dataReader["TPTDCONLAI"] is DBNull))
				this._TPTDConLai = Convert.ToDouble(dataReader["TPTDCONLAI"]);
			if (!(dataReader["TPTDCHITIETDATTRUOC"] is DBNull))
				this._TPTDChiTietDatTruoc = ((string)dataReader["TPTDCHITIETDATTRUOC"]).Trim();
			if (!(dataReader["CHTONGTIEN"] is DBNull))
				this._CHTongTien = Convert.ToDouble(dataReader["CHTONGTIEN"]);
			if (!(dataReader["CHDATTRUOC"] is DBNull))
				this._CHDatTruoc = Convert.ToDouble(dataReader["CHDATTRUOC"]);
			if (!(dataReader["CHCONLAI"] is DBNull))
				this._CHConLai = Convert.ToDouble(dataReader["CHCONLAI"]);
			if (!(dataReader["CHCHITIETDATTRUOC"] is DBNull))
				this._CHChiTietDatTruoc = ((string)dataReader["CHCHITIETDATTRUOC"]).Trim();
			if (!(dataReader["QPTONGTIEN"] is DBNull))
				this._QPTongTien = Convert.ToDouble(dataReader["QPTONGTIEN"]);
			if (!(dataReader["QPDATTRUOC"] is DBNull))
				this._QPDatTruoc = Convert.ToDouble(dataReader["QPDATTRUOC"]);
			if (!(dataReader["QPCONLAI"] is DBNull))
				this._QPConLai = Convert.ToDouble(dataReader["QPCONLAI"]);
			if (!(dataReader["QPCHITIETDATTRUOC"] is DBNull))
				this._QPChiTietDatTruoc = ((string)dataReader["QPCHITIETDATTRUOC"]).Trim();
			if (!(dataReader["DVKTONGTIEN"] is DBNull))
				this._DVKTongTien = Convert.ToDouble(dataReader["DVKTONGTIEN"]);
			if (!(dataReader["DVKDATTRUOC"] is DBNull))
				this._DVKDatTruoc = Convert.ToDouble(dataReader["DVKDATTRUOC"]);
			if (!(dataReader["DVKCONLAI"] is DBNull))
				this._DVKConLai = Convert.ToDouble(dataReader["DVKCONLAI"]);
			if (!(dataReader["DVKCHITIETDATTRUOC"] is DBNull))
				this._DVKChiTietDatTruoc = ((string)dataReader["DVKCHITIETDATTRUOC"]).Trim();
			if (!(dataReader["NGAYLAMHD"] is DBNull))
				this._NgayLamHD = (DateTime)dataReader["NGAYLAMHD"];
			if (!(dataReader["MINDAY"] is DBNull))
				this._MinDay = (DateTime)dataReader["MINDAY"];
			if (!(dataReader["MAXDAY"] is DBNull))
				this._MaxDay = (DateTime)dataReader["MAXDAY"];
			if (!(dataReader["MINDAYTT"] is DBNull))
				this._MinDayTT = (DateTime)dataReader["MINDAYTT"];
			if (!(dataReader["MAXDAYTT"] is DBNull))
				this._MaxDayTT = (DateTime)dataReader["MAXDAYTT"];
			if (!(dataReader["DELE"] is DBNull))
				this._DELE = (((string)dataReader["DELE"]).Trim() == "1");
		}
		#endregion Fetch
		//==========================================================================

		#region Insert
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Insert()
		{
			this.Execute_Insert();
		}

		protected void Execute_Insert()
		{
			this.This_Insert();
		}

		private void This_Insert()
		{
			if (this._TenKhachHang != null)
				this._TenKhachHang2 = this._TenKhachHang.ToUpper();

			using (FbConnection conn = new FbConnection(Session.CnnStr))
			{
				conn.Open();
				using (FbCommand cmd = conn.CreateCommand())
				{
					cmd.CommandText = "HopDongBO_I";
					cmd.CommandType = CommandType.StoredProcedure;
					cmd.Parameters.Add("@TENKHACHHANG", FbDbType.VarChar).Value = this._TenKhachHang;
					cmd.Parameters.Add("@TENKHACHHANG2", FbDbType.VarChar).Value = this._TenKhachHang2;
					cmd.Parameters.Add("@DIACHI", FbDbType.VarChar).Value = this._DiaChi;
					cmd.Parameters.Add("@DIENTHOAI", FbDbType.VarChar).Value = this._DienThoai;
					cmd.Parameters.Add("@NGAYCUOI", FbDbType.TimeStamp).Value = this._NgayCuoi;
					cmd.Parameters.Add("@CHUATHANHLY", FbDbType.Char).Value = (this._ChuaThanhLy.Value ? "1" : "0");
					cmd.Parameters.Add("@TONGGIATRIHD", FbDbType.Float).Value = this._TongGiaTriHD;
					cmd.Parameters.Add("@TONGGIATRIHDBC", FbDbType.VarChar).Value = this._TongGiaTriHDBC;
					cmd.Parameters.Add("@SOTIENDATTRUOC", FbDbType.Float).Value = this._SoTienDatTruoc;
					cmd.Parameters.Add("@SOTIENDATTRUOCBC", FbDbType.VarChar).Value = this._SoTienDatTruocBC;
					cmd.Parameters.Add("@SOTIENCONLAI", FbDbType.Float).Value = this._SoTienConLai;
					cmd.Parameters.Add("@SOTIENCONLAIBC", FbDbType.VarChar).Value = this._SoTienConLaiBC;
					cmd.Parameters.Add("@TPTDTONGTIEN", FbDbType.Float).Value = this._TPTDTongTien;
					cmd.Parameters.Add("@TPTDDATTRUOC", FbDbType.Float).Value = this._TPTDDatTruoc;
					cmd.Parameters.Add("@TPTDCONLAI", FbDbType.Float).Value = this._TPTDConLai;
					cmd.Parameters.Add("@TPTDCHITIETDATTRUOC", FbDbType.VarChar).Value = this._TPTDChiTietDatTruoc;
					cmd.Parameters.Add("@CHTONGTIEN", FbDbType.Float).Value = this._CHTongTien;
					cmd.Parameters.Add("@CHDATTRUOC", FbDbType.Float).Value = this._CHDatTruoc;
					cmd.Parameters.Add("@CHCONLAI", FbDbType.Float).Value = this._CHConLai;
					cmd.Parameters.Add("@CHCHITIETDATTRUOC", FbDbType.VarChar).Value = this._CHChiTietDatTruoc;
					cmd.Parameters.Add("@QPTONGTIEN", FbDbType.Float).Value = this._QPTongTien;
					cmd.Parameters.Add("@QPDATTRUOC", FbDbType.Float).Value = this._QPDatTruoc;
					cmd.Parameters.Add("@QPCONLAI", FbDbType.Float).Value = this._QPConLai;
					cmd.Parameters.Add("@QPCHITIETDATTRUOC", FbDbType.VarChar).Value = this._QPChiTietDatTruoc;
					cmd.Parameters.Add("@DVKTONGTIEN", FbDbType.Float).Value = this._DVKTongTien;
					cmd.Parameters.Add("@DVKDATTRUOC", FbDbType.Float).Value = this._DVKDatTruoc;
					cmd.Parameters.Add("@DVKCONLAI", FbDbType.Float).Value = this._DVKConLai;
					cmd.Parameters.Add("@DVKCHITIETDATTRUOC", FbDbType.VarChar).Value = this._DVKChiTietDatTruoc;
					cmd.Parameters.Add("@NGAYLAMHD", FbDbType.TimeStamp).Value = this._NgayLamHD;
					cmd.Parameters.Add("@MINDAY", FbDbType.TimeStamp).Value = this._MinDay;
					cmd.Parameters.Add("@MAXDAY", FbDbType.TimeStamp).Value = this._MaxDay;
					cmd.Parameters.Add("@MINDAYTT", FbDbType.TimeStamp).Value = this._MinDayTT;
					cmd.Parameters.Add("@MAXDAYTT", FbDbType.TimeStamp).Value = this._MaxDayTT;
					cmd.Parameters.Add("@DELE", FbDbType.Char).Value = (this._DELE.Value ? "1" : "0");

					FbParameter pHopDongID = new FbParameter("@HOPDONGID", FbDbType.Integer);
					pHopDongID.Direction = ParameterDirection.ReturnValue;
					cmd.Parameters.Add(pHopDongID);

					cmd.ExecuteNonQuery();
					this._HopDongID = Convert.ToInt32(cmd.Parameters["@HOPDONGID"].Value);
				}
			}
		}
		#endregion Insert
		//==========================================================================

		#region Update
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Update()
		{
			this.Execute_Update();
		}

		protected void Execute_Update()
		{
			this.This_Update();
		}

		private void This_Update()
		{
			if (this._TenKhachHang != null)
				this._TenKhachHang2 = this._TenKhachHang.ToUpper();

			using (FbConnection conn = new FbConnection(Session.CnnStr))
			{
				conn.Open();
				using (FbCommand cmd = conn.CreateCommand())
				{
					cmd.CommandText = "HopDongBO_U";
					cmd.CommandType = CommandType.StoredProcedure;
					cmd.Parameters.Add("@HOPDONGID", FbDbType.Integer).Value = this._HopDongID;
					cmd.Parameters.Add("@TENKHACHHANG", FbDbType.VarChar).Value = this._TenKhachHang;
					cmd.Parameters.Add("@TENKHACHHANG2", FbDbType.VarChar).Value = this._TenKhachHang2;
					cmd.Parameters.Add("@DIACHI", FbDbType.VarChar).Value = this._DiaChi;
					cmd.Parameters.Add("@DIENTHOAI", FbDbType.VarChar).Value = this._DienThoai;
					cmd.Parameters.Add("@NGAYCUOI", FbDbType.TimeStamp).Value = this._NgayCuoi;
					cmd.Parameters.Add("@CHUATHANHLY", FbDbType.Char).Value = (this._ChuaThanhLy.Value ? "1" : "0");
					cmd.Parameters.Add("@TONGGIATRIHD", FbDbType.Float).Value = this._TongGiaTriHD;
					cmd.Parameters.Add("@TONGGIATRIHDBC", FbDbType.VarChar).Value = this._TongGiaTriHDBC;
					cmd.Parameters.Add("@SOTIENDATTRUOC", FbDbType.Float).Value = this._SoTienDatTruoc;
					cmd.Parameters.Add("@SOTIENDATTRUOCBC", FbDbType.VarChar).Value = this._SoTienDatTruocBC;
					cmd.Parameters.Add("@SOTIENCONLAI", FbDbType.Float).Value = this._SoTienConLai;
					cmd.Parameters.Add("@SOTIENCONLAIBC", FbDbType.VarChar).Value = this._SoTienConLaiBC;
					cmd.Parameters.Add("@TPTDTONGTIEN", FbDbType.Float).Value = this._TPTDTongTien;
					cmd.Parameters.Add("@TPTDDATTRUOC", FbDbType.Float).Value = this._TPTDDatTruoc;
					cmd.Parameters.Add("@TPTDCONLAI", FbDbType.Float).Value = this._TPTDConLai;
					cmd.Parameters.Add("@TPTDCHITIETDATTRUOC", FbDbType.VarChar).Value = this._TPTDChiTietDatTruoc;
					cmd.Parameters.Add("@CHTONGTIEN", FbDbType.Float).Value = this._CHTongTien;
					cmd.Parameters.Add("@CHDATTRUOC", FbDbType.Float).Value = this._CHDatTruoc;
					cmd.Parameters.Add("@CHCONLAI", FbDbType.Float).Value = this._CHConLai;
					cmd.Parameters.Add("@CHCHITIETDATTRUOC", FbDbType.VarChar).Value = this._CHChiTietDatTruoc;
					cmd.Parameters.Add("@QPTONGTIEN", FbDbType.Float).Value = this._QPTongTien;
					cmd.Parameters.Add("@QPDATTRUOC", FbDbType.Float).Value = this._QPDatTruoc;
					cmd.Parameters.Add("@QPCONLAI", FbDbType.Float).Value = this._QPConLai;
					cmd.Parameters.Add("@QPCHITIETDATTRUOC", FbDbType.VarChar).Value = this._QPChiTietDatTruoc;
					cmd.Parameters.Add("@DVKTONGTIEN", FbDbType.Float).Value = this._DVKTongTien;
					cmd.Parameters.Add("@DVKDATTRUOC", FbDbType.Float).Value = this._DVKDatTruoc;
					cmd.Parameters.Add("@DVKCONLAI", FbDbType.Float).Value = this._DVKConLai;
					cmd.Parameters.Add("@DVKCHITIETDATTRUOC", FbDbType.VarChar).Value = this._DVKChiTietDatTruoc;
					cmd.Parameters.Add("@NGAYLAMHD", FbDbType.TimeStamp).Value = this._NgayLamHD;
					cmd.Parameters.Add("@MINDAY", FbDbType.TimeStamp).Value = this._MinDay;
					cmd.Parameters.Add("@MAXDAY", FbDbType.TimeStamp).Value = this._MaxDay;
					cmd.Parameters.Add("@MINDAYTT", FbDbType.TimeStamp).Value = this._MinDayTT;
					cmd.Parameters.Add("@MAXDAYTT", FbDbType.TimeStamp).Value = this._MaxDayTT;
					cmd.Parameters.Add("@DELE", FbDbType.Char).Value = (this._DELE.Value ? "1" : "0");

					cmd.ExecuteNonQuery();
				}
			}
		}
		#endregion Update
		//==========================================================================

		#region Delete
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_DeleteSelf()
		{
			DataPortal_Delete(new RootCriteria(_HopDongID.Value));
		}

		private void DataPortal_Delete(RootCriteria criteria)
		{
			this.Execute_Delete(criteria);
		}

		protected void Execute_Delete(RootCriteria criteria)
		{
			using (FbConnection conn = new FbConnection(Session.CnnStr))
			{
				conn.Open();
				using (FbCommand cmd = conn.CreateCommand())
				{
					if (criteria.directDelete)
					{
						cmd.CommandText = "DELETE FROM HOPDONG WHERE HOPDONGID = " + criteria.HopDongID + ";";
						cmd.ExecuteNonQuery();
					}
					else
					{
						cmd.CommandText = "UPDATE HOPDONG SET HOPDONG.DELE = '1' WHERE HOPDONGID = " + criteria.HopDongID + ";";
						cmd.ExecuteNonQuery();
					}
				}
			}
		}
		#endregion Delete
		//==========================================================================

		#region Internal method
		internal void Insert()
		{
			if (!this.IsDirty) return;
			DataPortal_Insert();
			MarkOld();
		}

		internal void Update()
		{
			if (!this.IsDirty) return;
			DataPortal_Update();
			MarkOld();
		}

		internal void DeleteSelf()
		{
			if (!this.IsDirty) return;
			if (this.IsNew) return;
			DataPortal_Delete(new RootCriteria(_HopDongID.Value));
			MarkNew();
		}
		#endregion Internal method
		//==========================================================================
	}
}